/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GestionVehiculo.java
 *
 * Created on 29/01/2011, 01:08:46 PM
 */

package com.catosist.run.application.vehiculo.gestion;

import com.catosist.run.application.Util.MiModelo;
import com.catosist.run.business.gestorvehiculo.GestorVehiculo;
import com.catosist.run.services.VehiculoDTO;
import java.util.Iterator;
import javax.swing.JPanel;
import javax.swing.table.DefaultTableModel;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Carlos
 */
public class GestionVehiculo extends javax.swing.JFrame {

    /** Creates new form GestionVehiculo */
    public GestionVehiculo() {
        initComponents();
    }

    public GestionVehiculo(GestorVehiculo gestorVehiculo) {
        initComponents();
        this.gestorVehiculo = gestorVehiculo;
        refresh();
    }

    public GestionVehiculo(boolean flag) {
        initComponents();
        iniciar();
        refresh();
    }

    private void iniciar() {
        ctx = new ClassPathXmlApplicationContext("/applicationContext.xml");
        gestorVehiculo = (GestorVehiculo) ctx.getBean("gestorVehiculo");

    }

    public void refresh() {
        //esta funcion va a cargar los datos exitentes

        DefaultTableModel model = new MiModelo();
        model.addColumn("Id");
        model.addColumn("Placa Vehiculo");
        model.addColumn("Combustible");
        model.addColumn("Marca");
        model.addColumn("Actividad");

        //model.addColumn("Fecha");
        //model.addColumn("Conductor");


        for (Iterator<VehiculoDTO> it = gestorVehiculo.getAll().iterator(); it.hasNext();) {
            VehiculoDTO vehiculoDTO = it.next();
            model.addRow(vehiculoDTO.toArray());
        }
        this.jTable1.setModel(model);
    }

    public JPanel getPanel() {
        return this.jPanel1;
    }

    public int getSelectedId() {
        int j = this.jTable1.getSelectedRow();
        return gestorVehiculo.getIdAtIndex(j);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setName("jPanel1"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jTable1.setName("jTable1"); // NOI18N
        jScrollPane1.setViewportView(jTable1);

        jLabel1.setText("LISTA DE VEHICULOS");
        jLabel1.setName("jLabel1"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(229, 229, 229)
                .addComponent(jLabel1)
                .addContainerGap(242, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 601, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 291, Short.MAX_VALUE)
                .addGap(12, 12, 12))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GestionVehiculo().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

    private GestorVehiculo gestorVehiculo;
    ApplicationContext ctx;
}
